//添加配置
var addhtml = ''
addhtml += '<div class="form-div"><form id="fomr">';
addhtml +='<div class="layui-form-item layui-form-select-icon iconfont"><label class="layui-form-label">货币</label><div class="layui-input-block"><select name="coin_id" lay-ignore class="layui-input   cion-type"></select></div></div>';
addhtml +='<div class="layui-form-item"><label class="layui-form-label">金额</label><div class="layui-input-block"><input type="text" name="config" autocomplete="off" class="layui-input" placeholder="请输入配置金额"></div></div>';
addhtml += '<input type="hidden" name="token" value="' + token + '">';
addhtml += '<input type="hidden" name="level_id">';
addhtml += '<input type="hidden" name="agent_type_id">';
addhtml += '<input type="hidden" name="type">';
addhtml += '</form></div>';


layui.use('layer', function() {
	var $ = layui.jquery,layer = layui.layer;
})

//添加配置token,type,coin_id,level_id,config
function add($this) {
	var level_id = $this.attr("data-id");
	var agent_type_id=$this.attr("data-agent");
	var title=$this.attr("data-name")+"配置";
	layer.open({
		type: 1,
		title:title,
		area: ['550px', '250px'], //宽高
		maxmin: true,
		resize: true,
		content:addhtml,
		btn: ['提交', '关闭'],
		btn1: function() {
			var Adata = $("#fomr").serialize();
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: addSrc,
				data: Adata,
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					return false;
					if(code == 1) {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		}
	});
	$("#fomr input[name=agent_type_id]").val(agent_type_id);
	$("#fomr input[name=type]").val(configType);
	$("#fomr input[name=level_id]").val(level_id);
	$("#fomr select.cion-type").html($("#cionHtml").html());
}


//删除配置

function del($this){
	var id=$this.attr("data-id");
	layer.alert('您确定要删除该配置！', {
		icon: 2,
		btn: ['确定', '关闭'],
		btn1: function() {
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: delSrc,
				data: {
					token: token,
					id: id
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		}
    })
}


//编辑
function edit($this){
	var id=$this.attr("data-id");
	var yconfig=$this.attr("data-config");
	var aval=$.trim($this.val());
	if(aval==yconfig){return false};
	layer.alert('您确定修改当前配置！', {
		icon: 2,
		btn: ['确定', '关闭'],
		btn1: function() {
			$.ajax({
				type: 'post',
				dataType: 'json',
				url: editSrc,
				data: {
					token: token,
					id: id,
					config:aval
				},
				success: function(data) {
					var code = data.code;
					var msg = data.msg;
					if(code == 1) {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 1
						}, function() {
							location.reload();
						})
					} else {
						layer.close(layer.index)
						layer.alert(msg, {
							icon: 5
						})
					}
				}
			})
		}
    })
	
}


function getlist(agent) {
	var agent_id=agent;
	$.ajax({
		type: 'post',
		dataType: 'json',
		url: listSrc,
		data:{
			id:agent_id
		},
		success: function(data) {
			var code = data.code;
			var data = data.data;
			if(code == 1) {
				var cenHtml = ""
				$.each(data, function(i, v) {
					var pzHtml = "";
					$.each(v.config, function(j, k) {
						pzHtml += '<div class="layui-row layui-pz-ht"><div class="layui-col-xs12"><div class="grid-cen"><div class="layui-form-item"><label class="layui-form-label">' + k.coin_name + '</label><div class="layui-input-inline"><input type="text" data-id="'+k.id+'" onblur="edit($(this))" name="config" autocomplete="off" data-config="' + k.config + '" value="' + k.config + '" class="layui-input"></div><div class="layui-input-inline"><a class="tab-cz-icon scicon" onclick="del($(this))" data-id="'+k.id+'" title="删除"></a></div></div></div></div></div>'
					});
					cenHtml += '<tr><td>' + v.level_name + '</td><td style="padding: 0;">' + pzHtml + '</td><td><a class="tab-cz-icon bingz" onclick="add($(this))" title="添加货币配置" data-id="' + v.id + '" data-name="'+ v.level_name +'" data-agent="'+agent_id+'"></a></td></tr>'

				});

				$(".center-tab .layui-table tbody").html(cenHtml);

			} else {
				layer.close(layer.index)
				layer.alert('列表获取失败', {
					icon: 5
				})
			}
		}
	});
}

$(function() {
	getlist($("select#agent_type option:selected").val());
	$("select#agent_type").change(function(){
		var agent=$("select#agent_type option:selected").val();
		getlist(agent);
	})
	//货币类型
	cionFun();
})